import requests,datetime,json,multiprocessing
import pandas as pd
import numpy as np

def init(l):
	global lock
	lock = l

def httpOp(req):
	i = 0
	while i < 4:
		try:
			d = None
			url, headersList, bodydict, method = req['url'], req['headersList'], req['bodydict'], req['method']
			res = requests.request(method, url, data=json.dumps(bodydict), headers=headersList)
			if res.status_code == 200:
				d = json.loads(res.text)
				if int(d['code']) != 200:
					print(d)
					print(bodydict)
					return False
				return d
			else:
				i += 1
				res.close()
				print(res.status_code, url, bodydict)
		except requests.exceptions.RequestException:
			i += 1
	return False
	
def start(gtyexports, i):
	try:
		tb_info = {
			"method": "POST",
			"url": "https://jg.landcloud.org.cn:8553/webapi/api/vlist",
			"headersList": {
				"Accept": "*/*",
				"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
				"Authorization": "",
				"Content-Type": "application/json"
			},
			"bodydict": {
				"ywlx": "ndbg2023",
				"xzqdm": "",
				"level": 1,
				"userxzqdm": "",
				"pageSize": 100000,
				"pageIndex": 1,
				"order": "",
				"isUseCacheCount": True,
				"optype": "2",
				"unitname": "",
				"loginsystem": "ndbg2023",
				"taskid": 85,
				"querydatatype": 1,
				"conditions": [
					{
						"fieldname": "jcbh",
						"conditiontype": 2,
						"values": [
							""
						],
						"useorquery": True
					}
				]
			}
		}

		zdk_info = {
			"method": "POST",
			"url": "https://jg.landcloud.org.cn:8553/webapi/zxjg/getdatarecord",
			"headersList": tb_info['headersList'],
			"bodydict": {
				"ywlx": "ndbg2023",
				"tbbsm": "",
				"nodeid": 1,
				"xzqdm": "",
				"containmain": True,
				"containshape": True,
				"subitemnames": [
					"hccg",
					"tbzt"
				],
				"sublistitemnames": [
					"yx",
					"zdk"
				],
				"exceptsubitemnames": [
					"tbzt"
				],
				"operate": "look",
				"exceptsublistitemnames": [
					"hcfj",
					"yx"
				],
				"ccywlx": None
			}
		}
		
		for row in gtyexports:
			tb_info['bodydict']['xzqdm'] = str(row['qxdm'])
			tb_info['bodydict']['conditions'][0]['values'][0] = row['tbbm']
			tbinfo = httpOp(tb_info)
			if tbinfo['data']['total'] > 1:
				print('big 1')
				for tb in tbinfo['data']['records']:
					if tb['jcbh'] == row['tbbm']:
						bsm = tb
			else:
				bsm = tbinfo['data']['records'][0]

			zdk_info['bodydict']['xzqdm'] = str(bsm['xzqdm'])
			zdk_info['bodydict']['tbbsm'] = str(bsm['tbbsm'])
			shapeinfo = httpOp(zdk_info)['data']
			zdks = shapeinfo['sublistitems']['zdk']
			for zdk in zdks:
				if not pd.isna(row['ctbh']):
					if int(zdk['dkbh']) == int(str(row['ctbh']).split('-')[1]):
						row['dkbh'] = zdk['dkbh']
						row['dkmj'] = zdk['dkmj']
						row['shape'] = zdk['dkshape']
				else:
					if zdk['dkshape'] == '':
						row['dkbh'] = zdk['dkbh']
						row['dkmj'] = shapeinfo['main']['jcmj']
						row['shape'] = shapeinfo['main']['shape']
					else:
						row['dkbh'] = zdk['dkbh']
						row['dkmj'] = zdk['dkmj']
						row['shape'] = zdk['dkshape']
		pd.DataFrame(gtyexports).to_csv('XXX项目清单_2023_1228_0514_{}.csv'.format(i))
		
	except Exception as err:
		print('err: ', err)
def main():
	try:
		splitn = 5
		gtyexports = pd.read_excel(io=r'XXX项目清单_2023_1228_0514.xlsx').copy()
		split_dts = np.array_split(gtyexports, splitn)
		
		cpucore = multiprocessing.cpu_count()
		mylock = multiprocessing.Lock()
		mpool = multiprocessing.Pool(cpucore, initializer = init, initargs = (mylock, ))

		for i in range(splitn):
			sdt = split_dts[i].to_dict(orient='records')
			mpool.apply_async(start, args=(sdt, i, ))

		print('{}mpool start here.{}'.format('\n', '\n'))
		mpool.close()
		mpool.join()
		print('mpool end here.')
		
	except Exception as err:
		print('err: ', err)

if __name__ == '__main__':
	time1 = datetime.datetime.now()
	print('start time: ', time1)
	main()
	time2 = datetime.datetime.now()
	print('end time: ', time2)
	print('total time: ', time2 - time1)